home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / Samples / SprocketExamples / GlyphaIV / GlyphaIV Sources / G4Main.c < prev    next >
Encoding:
Text File  |  1998-07-14  |  4.0 KB  |  153 lines  |  [TEXT/CWIE]

  1.  
  2. //============================================================================
  3. //----------------------------------------------------------------------------
  4. //                                Glypha IV 1.0
  5. //                                by  Scheherazade
  6. //----------------------------------------------------------------------------
  7. //============================================================================
  8.  
  9. // Here is the "main" file for Glypha.  Here is where the game begins and ends.
  10. // Also included are the preference calls.
  11.  
  12. #include "G4Externs.h"
  13. #include <Sound.h>
  14.  
  15.  
  16. #define kPrefsVersion            0x0001
  17.  
  18.  
  19. void ReadInPrefs (void);
  20. void WriteOutPrefs (void);
  21. void main (void);
  22.  
  23.  
  24. prefsInfo    thePrefs;
  25. short        wasVolume;
  26.  
  27. extern    Boolean        quitting, playing, pausing, evenFrame;
  28. void UpdateMainWindow (void);
  29.  
  30. //==============================================================  Functions
  31. //--------------------------------------------------------------  ReadInPrefs
  32.  
  33. //    This function loads up the preferences.  If the preferences 
  34. //    aren't found, all settings are set to their defaults.
  35.  
  36. void ReadInPrefs (void)
  37. {
  38.     short        i;
  39.                             // Call LoadPrefs() function - returns TRUE if it worked.
  40.     if (LoadPrefs(&thePrefs, kPrefsVersion))
  41.         SetSoundVol(thePrefs.wasVolume);
  42.     else                    // If LoadPrefs() failed, set defaults.
  43.     {
  44.         thePrefs.prefVersion = kPrefsVersion;                // version of prefs
  45.         thePrefs.filler = 0;                                // just padding
  46.         PasStringCopy("\pYour Name", thePrefs.highName);    // last highscores name
  47.         for (i = 0; i < 10; i++)                            // loop through scores
  48.         {
  49.             PasStringCopy("\pNemo", thePrefs.highNames[i]);    // put "Nemo" in name
  50.             thePrefs.highScores[i] = 0L;                    // set highscore to 0
  51.             thePrefs.highLevel[i] = 0;                        // level attained = 0
  52.         }
  53.         GetSoundVol(&thePrefs.wasVolume);
  54.     }
  55.                             // Get sound volume so we can restore it.
  56.     GetSoundVol(&wasVolume);
  57. }
  58.  
  59. //--------------------------------------------------------------  WriteOutPrefs
  60.  
  61. //    This function writes out the preferences to disk and restores 
  62. //    the sound volume to its setting before Glypha was launched.
  63.  
  64. void WriteOutPrefs (void)
  65. {
  66.     if (!SavePrefs(&thePrefs, kPrefsVersion))
  67.         SysBeep(1);
  68.     SetSoundVol(wasVolume);
  69. }
  70.  
  71. //--------------------------------------------------------------  main
  72.  
  73. //    This is the main function.  Every C program has one of these.
  74. //    First it initializes our program and then falls into a loop
  75. //    until the user chooses to quit.  At that point, it cleans up
  76. //    and exits.
  77.  
  78. void main (void)
  79. {
  80.     long        tickWait;
  81.     OSStatus    theError;
  82.     Boolean        wasPlaying = false;
  83.     
  84.     // startup DrawSprocket
  85.     theError = DSpStartup();
  86.     if( theError )
  87.         RedAlert("\punable to init DrawSprocket!");
  88.     // DSpSetDebugMode( true );
  89.     
  90.     ToolBoxInit();            // Call function that initializes the ToolBox managers.
  91.     CheckEnvirons();        // Check the Mac we're on to see if we can run.
  92.     OpenMainWindow();        // Open up the main window - it will fill the monitor.
  93.     InitVariables();        // Initialize Glypha's variables.
  94.     InitSound();            // Create sound channels and load up sounds.
  95.     InitMenubar();            // Set up the game's menubar.
  96.     ReadInPrefs();            // Load up the preferences.
  97.  
  98. #if GENERATINGPOWERPC
  99.     UpdateMainWindow();
  100.  
  101.     theError = DSpContext_FadeGammaIn( NULL, NULL );
  102.     if( theError )
  103.         RedAlert("\pUnable to unfade the display!");
  104.     
  105. #endif
  106.  
  107.     do                        // Here begins the main loop.
  108.     {
  109.     
  110.         HandleEvent();        // Check for events.
  111.         
  112.         if ((playing) && (!pausing))
  113.         {
  114.             wasPlaying = true;
  115.             HideCursor();
  116.             PlayGame();        // If user began game, drop in game loop. (play mode)
  117.             ShowCursor();
  118.         }
  119.         else if (pausing && playing)
  120.         {
  121.         }
  122.         else                // If no game, animate the screen. (idle mode)
  123.         {
  124.             if (Button())
  125.             {
  126.                 Point pt;
  127.                 
  128.                 GetMouse(&pt);
  129.                 pt.h += (Random() % 21) - 10;
  130.                 pt.v += (Random() % 21) - 10;
  131.                 StartPixelShatter(pt.h, pt.v, 0, 0, kShatterLightningDust);
  132.             }
  133.             
  134.             if (wasPlaying)
  135.             {
  136.                 GameQuitGraphics();
  137.                 wasPlaying = false;
  138.             }
  139.             tickWait = TickCount() + 2L;
  140.             GameIdleAnimation();
  141.  
  142.             evenFrame = !evenFrame;
  143.         }
  144.     }
  145.     while (!quitting);
  146.     
  147.     KillSound();            // Dispose of sound channels.
  148.     ShutItDown();            // Dispose of other structures.
  149.     WriteOutPrefs();        // Save preferences to disk.
  150.  
  151. }
  152.  
  153.